package com.eaoa.uc.account.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.eaoa.base.EABaseAction;
import com.eaoa.query.pub.PubAccountQuery;
import com.eaoa.uc.customer.form.CustomerCondition;
import com.eaoa.uc.customer.form.CustomerSearchForm;
import com.eaoa.vo.pub.PubAccount;
import com.stony.core.util.CoreUtils;

public class AccountSelectCustomersAction extends EABaseAction{

	protected ActionForward doWork(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		CustomerSearchForm sform = (CustomerSearchForm) form;
		sform.initConditionPost();
		CustomerCondition condition = (CustomerCondition) sform.getCondition();
		
		Long accountno = CoreUtils.parseLong(request.getParameter("accountno"));
		
//		PubAccount account = PubAccountQuery.loadByNoInitPubCustomers(accountno);
		
		PubAccountQuery query = new PubAccountQuery();
		query.setEmployeeno(accountno);
		query.setCustomerCode(condition.getCustomercode());
		query.setCustomerName(condition.getName());
		query.setCustomerType(condition.getCustomertype());
		query.setCustomerIslock(condition.getIslock());
		
		query.setLazy(true);
		query.setLazyInitPubCustomers(true);
		query.execute();
		
		List results = query.getResults();
		
		if(results.size()!=1)
			return mapping.findForward("input");
		PubAccount account = (PubAccount)results.get(0);
		
		request.setAttribute("accountno", accountno);
		request.setAttribute("customers", account.getPubCustomers());
		
		return mapping.findForward("success");
	}
}
